<?php


/**
 * Base class that represents a row from the 'applications' table.
 *
 *
 *
 * @package    propel.generator..om
 */
abstract class BaseApplications extends BaseObject implements Persistent
{
    /**
     * Peer class name
     */
    const PEER = 'ApplicationsPeer';

    /**
     * The Peer class.
     * Instance provides a convenient way of calling static methods on a class
     * that calling code may not be able to identify.
     * @var        ApplicationsPeer
     */
    protected static $peer;

    /**
     * The flag var to prevent infinit loop in deep copy
     * @var       boolean
     */
    protected $startCopy = false;

    /**
     * The value for the id field.
     * @var        int
     */
    protected $id;

    /**
     * The value for the name field.
     * @var        string
     */
    protected $name;

    /**
     * The value for the description field.
     * @var        string
     */
    protected $description;

    /**
     * The value for the app_id field.
     * @var        string
     */
    protected $app_id;

    /**
     * The value for the app_key field.
     * @var        string
     */
    protected $app_key;

    /**
     * The value for the admin_key field.
     * @var        string
     */
    protected $admin_key;

    /**
     * The value for the to_modified field.
     * Note: this column has a database default value of: 0
     * @var        int
     */
    protected $to_modified;

    /**
     * The value for the active field.
     * Note: this column has a database default value of: 1
     * @var        int
     */
    protected $active;

    /**
     * The value for the last_modified field.
     * @var        string
     */
    protected $last_modified;

    /**
     * The value for the created field.
     * Note: this column has a database default value of: '2014-04-25 17:06:21'
     * @var        string
     */
    protected $created;

    /**
     * @var        PropelObjectCollection|UserPulpit[] Collection to store aggregation of UserPulpit objects.
     */
    protected $collUserPulpits;
    protected $collUserPulpitsPartial;

    /**
     * @var        PropelObjectCollection|ApplicationDataSets[] Collection to store aggregation of ApplicationDataSets objects.
     */
    protected $collApplicationDataSetss;
    protected $collApplicationDataSetssPartial;

    /**
     * @var        PropelObjectCollection|UserGroupApplications[] Collection to store aggregation of UserGroupApplications objects.
     */
    protected $collUserGroupApplicationss;
    protected $collUserGroupApplicationssPartial;

    /**
     * Flag to prevent endless save loop, if this object is referenced
     * by another object which falls in this transaction.
     * @var        boolean
     */
    protected $alreadyInSave = false;

    /**
     * Flag to prevent endless validation loop, if this object is referenced
     * by another object which falls in this transaction.
     * @var        boolean
     */
    protected $alreadyInValidation = false;

    /**
     * An array of objects scheduled for deletion.
     * @var		PropelObjectCollection
     */
    protected $userPulpitsScheduledForDeletion = null;

    /**
     * An array of objects scheduled for deletion.
     * @var		PropelObjectCollection
     */
    protected $applicationDataSetssScheduledForDeletion = null;

    /**
     * An array of objects scheduled for deletion.
     * @var		PropelObjectCollection
     */
    protected $userGroupApplicationssScheduledForDeletion = null;

    /**
     * Applies default values to this object.
     * This method should be called from the object's constructor (or
     * equivalent initialization method).
     * @see        __construct()
     */
    public function applyDefaultValues()
    {
        $this->to_modified = 0;
        $this->active = 1;
        $this->created = '2014-04-25 17:06:21';
    }

    /**
     * Initializes internal state of BaseApplications object.
     * @see        applyDefaults()
     */
    public function __construct()
    {
        parent::__construct();
        $this->applyDefaultValues();
    }

    /**
     * Get the [id] column value.
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Get the [name] column value.
     *
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Get the [description] column value.
     *
     * @return string
     */
    public function getDescription()
    {
        return $this->description;
    }

    /**
     * Get the [app_id] column value.
     *
     * @return string
     */
    public function getAppId()
    {
        return $this->app_id;
    }

    /**
     * Get the [app_key] column value.
     *
     * @return string
     */
    public function getAppKey()
    {
        return $this->app_key;
    }

    /**
     * Get the [admin_key] column value.
     *
     * @return string
     */
    public function getAdminKey()
    {
        return $this->admin_key;
    }

    /**
     * Get the [to_modified] column value.
     *
     * @return int
     */
    public function getToModified()
    {
        return $this->to_modified;
    }

    /**
     * Get the [active] column value.
     *
     * @return int
     */
    public function getActive()
    {
        return $this->active;
    }

    /**
     * Get the [optionally formatted] temporal [last_modified] column value.
     *
     *
     * @param string $format The date/time format string (either date()-style or strftime()-style).
     *				 If format is null, then the raw DateTime object will be returned.
     * @return mixed Formatted date/time value as string or DateTime object (if format is null), null if column is null
     * @throws PropelException - if unable to parse/validate the date/time value.
     */
    public function getLastModified($format = 'Y-m-d H:i:s')
    {
        if ($this->last_modified === null) {
            return null;
        }


        try {
            $dt = new DateTime($this->last_modified);
        } catch (Exception $x) {
            throw new PropelException("Internally stored date/time/timestamp value could not be converted to DateTime: " . var_export($this->last_modified, true), $x);
        }

        if ($format === null) {
            // Because propel.useDateTimeClass is true, we return a DateTime object.
            return $dt;
        } elseif (strpos($format, '%') !== false) {
            return strftime($format, $dt->format('U'));
        } else {
            return $dt->format($format);
        }
    }

    /**
     * Get the [optionally formatted] temporal [created] column value.
     *
     *
     * @param string $format The date/time format string (either date()-style or strftime()-style).
     *				 If format is null, then the raw DateTime object will be returned.
     * @return mixed Formatted date/time value as string or DateTime object (if format is null), null if column is null
     * @throws PropelException - if unable to parse/validate the date/time value.
     */
    public function getCreated($format = 'Y-m-d H:i:s')
    {
        if ($this->created === null) {
            return null;
        }


        try {
            $dt = new DateTime($this->created);
        } catch (Exception $x) {
            throw new PropelException("Internally stored date/time/timestamp value could not be converted to DateTime: " . var_export($this->created, true), $x);
        }

        if ($format === null) {
            // Because propel.useDateTimeClass is true, we return a DateTime object.
            return $dt;
        } elseif (strpos($format, '%') !== false) {
            return strftime($format, $dt->format('U'));
        } else {
            return $dt->format($format);
        }
    }

    /**
     * Set the value of [id] column.
     *
     * @param int $v new value
     * @return Applications The current object (for fluent API support)
     */
    public function setId($v)
    {
        if ($v !== null) {
            $v = (int) $v;
        }

        if ($this->id !== $v) {
            $this->id = $v;
            $this->modifiedColumns[] = ApplicationsPeer::ID;
        }


        return $this;
    } // setId()

    /**
     * Set the value of [name] column.
     *
     * @param string $v new value
     * @return Applications The current object (for fluent API support)
     */
    public function setName($v)
    {
        if ($v !== null) {
            $v = (string) $v;
        }

        if ($this->name !== $v) {
            $this->name = $v;
            $this->modifiedColumns[] = ApplicationsPeer::NAME;
        }


        return $this;
    } // setName()

    /**
     * Set the value of [description] column.
     *
     * @param string $v new value
     * @return Applications The current object (for fluent API support)
     */
    public function setDescription($v)
    {
        if ($v !== null) {
            $v = (string) $v;
        }

        if ($this->description !== $v) {
            $this->description = $v;
            $this->modifiedColumns[] = ApplicationsPeer::DESCRIPTION;
        }


        return $this;
    } // setDescription()

    /**
     * Set the value of [app_id] column.
     *
     * @param string $v new value
     * @return Applications The current object (for fluent API support)
     */
    public function setAppId($v)
    {
        if ($v !== null) {
            $v = (string) $v;
        }

        if ($this->app_id !== $v) {
            $this->app_id = $v;
            $this->modifiedColumns[] = ApplicationsPeer::APP_ID;
        }


        return $this;
    } // setAppId()

    /**
     * Set the value of [app_key] column.
     *
     * @param string $v new value
     * @return Applications The current object (for fluent API support)
     */
    public function setAppKey($v)
    {
        if ($v !== null) {
            $v = (string) $v;
        }

        if ($this->app_key !== $v) {
            $this->app_key = $v;
            $this->modifiedColumns[] = ApplicationsPeer::APP_KEY;
        }


        return $this;
    } // setAppKey()

    /**
     * Set the value of [admin_key] column.
     *
     * @param string $v new value
     * @return Applications The current object (for fluent API support)
     */
    public function setAdminKey($v)
    {
        if ($v !== null) {
            $v = (string) $v;
        }

        if ($this->admin_key !== $v) {
            $this->admin_key = $v;
            $this->modifiedColumns[] = ApplicationsPeer::ADMIN_KEY;
        }


        return $this;
    } // setAdminKey()

    /**
     * Set the value of [to_modified] column.
     *
     * @param int $v new value
     * @return Applications The current object (for fluent API support)
     */
    public function setToModified($v)
    {
        if ($v !== null) {
            $v = (int) $v;
        }

        if ($this->to_modified !== $v) {
            $this->to_modified = $v;
            $this->modifiedColumns[] = ApplicationsPeer::TO_MODIFIED;
        }


        return $this;
    } // setToModified()

    /**
     * Set the value of [active] column.
     *
     * @param int $v new value
     * @return Applications The current object (for fluent API support)
     */
    public function setActive($v)
    {
        if ($v !== null) {
            $v = (int) $v;
        }

        if ($this->active !== $v) {
            $this->active = $v;
            $this->modifiedColumns[] = ApplicationsPeer::ACTIVE;
        }


        return $this;
    } // setActive()

    /**
     * Sets the value of [last_modified] column to a normalized version of the date/time value specified.
     *
     * @param mixed $v string, integer (timestamp), or DateTime value.
     *               Empty strings are treated as null.
     * @return Applications The current object (for fluent API support)
     */
    public function setLastModified($v)
    {
        $dt = PropelDateTime::newInstance($v, null, 'DateTime');
        if ($this->last_modified !== null || $dt !== null) {
            $currentDateAsString = ($this->last_modified !== null && $tmpDt = new DateTime($this->last_modified)) ? $tmpDt->format('Y-m-d H:i:s') : null;
            $newDateAsString = $dt ? $dt->format('Y-m-d H:i:s') : null;
            if ($currentDateAsString !== $newDateAsString) {
                $this->last_modified = $newDateAsString;
                $this->modifiedColumns[] = ApplicationsPeer::LAST_MODIFIED;
            }
        } // if either are not null


        return $this;
    } // setLastModified()

    /**
     * Sets the value of [created] column to a normalized version of the date/time value specified.
     *
     * @param mixed $v string, integer (timestamp), or DateTime value.
     *               Empty strings are treated as null.
     * @return Applications The current object (for fluent API support)
     */
    public function setCreated($v)
    {
        $dt = PropelDateTime::newInstance($v, null, 'DateTime');
        if ($this->created !== null || $dt !== null) {
            $currentDateAsString = ($this->created !== null && $tmpDt = new DateTime($this->created)) ? $tmpDt->format('Y-m-d H:i:s') : null;
            $newDateAsString = $dt ? $dt->format('Y-m-d H:i:s') : null;
            if ( ($currentDateAsString !== $newDateAsString) // normalized values don't match
                || ($dt->format('Y-m-d H:i:s') === '2014-04-25 17:06:21') // or the entered value matches the default
                 ) {
                $this->created = $newDateAsString;
                $this->modifiedColumns[] = ApplicationsPeer::CREATED;
            }
        } // if either are not null


        return $this;
    } // setCreated()

    /**
     * Indicates whether the columns in this object are only set to default values.
     *
     * This method can be used in conjunction with isModified() to indicate whether an object is both
     * modified _and_ has some values set which are non-default.
     *
     * @return boolean Whether the columns in this object are only been set with default values.
     */
    public function hasOnlyDefaultValues()
    {
            if ($this->to_modified !== 0) {
                return false;
            }

            if ($this->active !== 1) {
                return false;
            }

            if ($this->created !== '2014-04-25 17:06:21') {
                return false;
            }

        // otherwise, everything was equal, so return true
        return true;
    } // hasOnlyDefaultValues()

    /**
     * Hydrates (populates) the object variables with values from the database resultset.
     *
     * An offset (0-based "start column") is specified so that objects can be hydrated
     * with a subset of the columns in the resultset rows.  This is needed, for example,
     * for results of JOIN queries where the resultset row includes columns from two or
     * more tables.
     *
     * @param array $row The row returned by PDOStatement->fetch(PDO::FETCH_NUM)
     * @param int $startcol 0-based offset column which indicates which restultset column to start with.
     * @param boolean $rehydrate Whether this object is being re-hydrated from the database.
     * @return int             next starting column
     * @throws PropelException - Any caught Exception will be rewrapped as a PropelException.
     */
    public function hydrate($row, $startcol = 0, $rehydrate = false)
    {
        try {

            $this->id = ($row[$startcol + 0] !== null) ? (int) $row[$startcol + 0] : null;
            $this->name = ($row[$startcol + 1] !== null) ? (string) $row[$startcol + 1] : null;
            $this->description = ($row[$startcol + 2] !== null) ? (string) $row[$startcol + 2] : null;
            $this->app_id = ($row[$startcol + 3] !== null) ? (string) $row[$startcol + 3] : null;
            $this->app_key = ($row[$startcol + 4] !== null) ? (string) $row[$startcol + 4] : null;
            $this->admin_key = ($row[$startcol + 5] !== null) ? (string) $row[$startcol + 5] : null;
            $this->to_modified = ($row[$startcol + 6] !== null) ? (int) $row[$startcol + 6] : null;
            $this->active = ($row[$startcol + 7] !== null) ? (int) $row[$startcol + 7] : null;
            $this->last_modified = ($row[$startcol + 8] !== null) ? (string) $row[$startcol + 8] : null;
            $this->created = ($row[$startcol + 9] !== null) ? (string) $row[$startcol + 9] : null;
            $this->resetModified();

            $this->setNew(false);

            if ($rehydrate) {
                $this->ensureConsistency();
            }

            return $startcol + 10; // 10 = ApplicationsPeer::NUM_HYDRATE_COLUMNS.

        } catch (Exception $e) {
            throw new PropelException("Error populating Applications object", $e);
        }
    }

    /**
     * Checks and repairs the internal consistency of the object.
     *
     * This method is executed after an already-instantiated object is re-hydrated
     * from the database.  It exists to check any foreign keys to make sure that
     * the objects related to the current object are correct based on foreign key.
     *
     * You can override this method in the stub class, but you should always invoke
     * the base method from the overridden method (i.e. parent::ensureConsistency()),
     * in case your model changes.
     *
     * @throws PropelException
     */
    public function ensureConsistency()
    {

    } // ensureConsistency

    /**
     * Reloads this object from datastore based on primary key and (optionally) resets all associated objects.
     *
     * This will only work if the object has been saved and has a valid primary key set.
     *
     * @param boolean $deep (optional) Whether to also de-associated any related objects.
     * @param PropelPDO $con (optional) The PropelPDO connection to use.
     * @return void
     * @throws PropelException - if this object is deleted, unsaved or doesn't have pk match in db
     */
    public function reload($deep = false, PropelPDO $con = null)
    {
        if ($this->isDeleted()) {
            throw new PropelException("Cannot reload a deleted object.");
        }

        if ($this->isNew()) {
            throw new PropelException("Cannot reload an unsaved object.");
        }

        if ($con === null) {
            $con = Propel::getConnection(ApplicationsPeer::DATABASE_NAME, Propel::CONNECTION_READ);
        }

        // We don't need to alter the object instance pool; we're just modifying this instance
        // already in the pool.

        $stmt = ApplicationsPeer::doSelectStmt($this->buildPkeyCriteria(), $con);
        $row = $stmt->fetch(PDO::FETCH_NUM);
        $stmt->closeCursor();
        if (!$row) {
            throw new PropelException('Cannot find matching row in the database to reload object values.');
        }
        $this->hydrate($row, 0, true); // rehydrate

        if ($deep) {  // also de-associate any related objects?

            $this->collUserPulpits = null;

            $this->collApplicationDataSetss = null;

            $this->collUserGroupApplicationss = null;

        } // if (deep)
    }

    /**
     * Removes this object from datastore and sets delete attribute.
     *
     * @param PropelPDO $con
     * @return void
     * @throws PropelException
     * @throws Exception
     * @see        BaseObject::setDeleted()
     * @see        BaseObject::isDeleted()
     */
    public function delete(PropelPDO $con = null)
    {
        if ($this->isDeleted()) {
            throw new PropelException("This object has already been deleted.");
        }

        if ($con === null) {
            $con = Propel::getConnection(ApplicationsPeer::DATABASE_NAME, Propel::CONNECTION_WRITE);
        }

        $con->beginTransaction();
        try {
            $deleteQuery = ApplicationsQuery::create()
                ->filterByPrimaryKey($this->getPrimaryKey());
            $ret = $this->preDelete($con);
            if ($ret) {
                $deleteQuery->delete($con);
                $this->postDelete($con);
                $con->commit();
                $this->setDeleted(true);
            } else {
                $con->commit();
            }
        } catch (Exception $e) {
            $con->rollBack();
            throw $e;
        }
    }

    /**
     * Persists this object to the database.
     *
     * If the object is new, it inserts it; otherwise an update is performed.
     * All modified related objects will also be persisted in the doSave()
     * method.  This method wraps all precipitate database operations in a
     * single transaction.
     *
     * @param PropelPDO $con
     * @return int             The number of rows affected by this insert/update and any referring fk objects' save() operations.
     * @throws PropelException
     * @throws Exception
     * @see        doSave()
     */
    public function save(PropelPDO $con = null)
    {
        if ($this->isDeleted()) {
            throw new PropelException("You cannot save an object that has been deleted.");
        }

        if ($con === null) {
            $con = Propel::getConnection(ApplicationsPeer::DATABASE_NAME, Propel::CONNECTION_WRITE);
        }

        $con->beginTransaction();
        $isInsert = $this->isNew();
        try {
            $ret = $this->preSave($con);
            if ($isInsert) {
                $ret = $ret && $this->preInsert($con);
            } else {
                $ret = $ret && $this->preUpdate($con);
            }
            if ($ret) {
                $affectedRows = $this->doSave($con);
                if ($isInsert) {
                    $this->postInsert($con);
                } else {
                    $this->postUpdate($con);
                }
                $this->postSave($con);
                ApplicationsPeer::addInstanceToPool($this);
            } else {
                $affectedRows = 0;
            }
            $con->commit();

            return $affectedRows;
        } catch (Exception $e) {
            $con->rollBack();
            throw $e;
        }
    }

    /**
     * Performs the work of inserting or updating the row in the database.
     *
     * If the object is new, it inserts it; otherwise an update is performed.
     * All related objects are also updated in this method.
     *
     * @param PropelPDO $con
     * @return int             The number of rows affected by this insert/update and any referring fk objects' save() operations.
     * @throws PropelException
     * @see        save()
     */
    protected function doSave(PropelPDO $con)
    {
        $affectedRows = 0; // initialize var to track total num of affected rows
        if (!$this->alreadyInSave) {
            $this->alreadyInSave = true;

            if ($this->isNew() || $this->isModified()) {
                // persist changes
                if ($this->isNew()) {
                    $this->doInsert($con);
                } else {
                    $this->doUpdate($con);
                }
                $affectedRows += 1;
                $this->resetModified();
            }

            if ($this->userPulpitsScheduledForDeletion !== null) {
                if (!$this->userPulpitsScheduledForDeletion->isEmpty()) {
                    UserPulpitQuery::create()
                        ->filterByPrimaryKeys($this->userPulpitsScheduledForDeletion->getPrimaryKeys(false))
                        ->delete($con);
                    $this->userPulpitsScheduledForDeletion = null;
                }
            }

            if ($this->collUserPulpits !== null) {
                foreach ($this->collUserPulpits as $referrerFK) {
                    if (!$referrerFK->isDeleted()) {
                        $affectedRows += $referrerFK->save($con);
                    }
                }
            }

            if ($this->applicationDataSetssScheduledForDeletion !== null) {
                if (!$this->applicationDataSetssScheduledForDeletion->isEmpty()) {
                    ApplicationDataSetsQuery::create()
                        ->filterByPrimaryKeys($this->applicationDataSetssScheduledForDeletion->getPrimaryKeys(false))
                        ->delete($con);
                    $this->applicationDataSetssScheduledForDeletion = null;
                }
            }

            if ($this->collApplicationDataSetss !== null) {
                foreach ($this->collApplicationDataSetss as $referrerFK) {
                    if (!$referrerFK->isDeleted()) {
                        $affectedRows += $referrerFK->save($con);
                    }
                }
            }

            if ($this->userGroupApplicationssScheduledForDeletion !== null) {
                if (!$this->userGroupApplicationssScheduledForDeletion->isEmpty()) {
                    UserGroupApplicationsQuery::create()
                        ->filterByPrimaryKeys($this->userGroupApplicationssScheduledForDeletion->getPrimaryKeys(false))
                        ->delete($con);
                    $this->userGroupApplicationssScheduledForDeletion = null;
                }
            }

            if ($this->collUserGroupApplicationss !== null) {
                foreach ($this->collUserGroupApplicationss as $referrerFK) {
                    if (!$referrerFK->isDeleted()) {
                        $affectedRows += $referrerFK->save($con);
                    }
                }
            }

            $this->alreadyInSave = false;

        }

        return $affectedRows;
    } // doSave()

    /**
     * Insert the row in the database.
     *
     * @param PropelPDO $con
     *
     * @throws PropelException
     * @see        doSave()
     */
    protected function doInsert(PropelPDO $con)
    {
        $this->modifiedColumns[] = ApplicationsPeer::ID;
        $criteria = $this->buildCriteria();
        if ($criteria->keyContainsValue(ApplicationsPeer::ID) ) {
            throw new PropelException('Cannot insert a value for auto-increment primary key (' . ApplicationsPeer::ID . ')');
        }
        // remove pkey col since this table uses auto-increment and passing a null value for it is not valid
        $criteria->remove(ApplicationsPeer::ID);
        $pk = BasePeer::doInsert($criteria, $con);
        $this->setId($pk);  //[IMV] update autoincrement primary key
        $this->setNew(false);
    }

    /**
     * Update the row in the database.
     *
     * @param PropelPDO $con
     *
     * @see        doSave()
     */
    protected function doUpdate(PropelPDO $con)
    {
        $selectCriteria = $this->buildPkeyCriteria();
        $valuesCriteria = $this->buildCriteria();
        BasePeer::doUpdate($selectCriteria, $valuesCriteria, $con);
    }

    /**
     * Array of ValidationFailed objects.
     * @var        array ValidationFailed[]
     */
    protected $validationFailures = array();

    /**
     * Gets any ValidationFailed objects that resulted from last call to validate().
     *
     *
     * @return array ValidationFailed[]
     * @see        validate()
     */
    public function getValidationFailures()
    {
        return $this->validationFailures;
    }

    /**
     * Validates the objects modified field values and all objects related to this table.
     *
     * If $columns is either a column name or an array of column names
     * only those columns are validated.
     *
     * @param mixed $columns Column name or an array of column names.
     * @return boolean Whether all columns pass validation.
     * @see        doValidate()
     * @see        getValidationFailures()
     */
    public function validate($columns = null)
    {
        $res = $this->doValidate($columns);
        if ($res === true) {
            $this->validationFailures = array();

            return true;
        } else {
            $this->validationFailures = $res;

            return false;
        }
    }

    /**
     * This function performs the validation work for complex object models.
     *
     * In addition to checking the current object, all related objects will
     * also be validated.  If all pass then <code>true</code> is returned; otherwise
     * an aggreagated array of ValidationFailed objects will be returned.
     *
     * @param array $columns Array of column names to validate.
     * @return mixed <code>true</code> if all validations pass; array of <code>ValidationFailed</code> objets otherwise.
     */
    protected function doValidate($columns = null)
    {
        if (!$this->alreadyInValidation) {
            $this->alreadyInValidation = true;
            $retval = null;

            $failureMap = array();


            if (($retval = ApplicationsPeer::doValidate($this, $columns)) !== true) {
                $failureMap = array_merge($failureMap, $retval);
            }


                if ($this->collUserPulpits !== null) {
                    foreach ($this->collUserPulpits as $referrerFK) {
                        if (!$referrerFK->validate($columns)) {
                            $failureMap = array_merge($failureMap, $referrerFK->getValidationFailures());
                        }
                    }
                }

                if ($this->collApplicationDataSetss !== null) {
                    foreach ($this->collApplicationDataSetss as $referrerFK) {
                        if (!$referrerFK->validate($columns)) {
                            $failureMap = array_merge($failureMap, $referrerFK->getValidationFailures());
                        }
                    }
                }

                if ($this->collUserGroupApplicationss !== null) {
                    foreach ($this->collUserGroupApplicationss as $referrerFK) {
                        if (!$referrerFK->validate($columns)) {
                            $failureMap = array_merge($failureMap, $referrerFK->getValidationFailures());
                        }
                    }
                }


            $this->alreadyInValidation = false;
        }

        return (!empty($failureMap) ? $failureMap : true);
    }

    /**
     * Retrieves a field from the object by name passed in as a string.
     *
     * @param string $name name
     * @param string $type The type of fieldname the $name is of:
     *               one of the class type constants BasePeer::TYPE_PHPNAME, BasePeer::TYPE_STUDLYPHPNAME
     *               BasePeer::TYPE_COLNAME, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_NUM.
     *               Defaults to BasePeer::TYPE_PHPNAME
     * @return mixed Value of field.
     */
    public function getByName($name, $type = BasePeer::TYPE_PHPNAME)
    {
        $pos = ApplicationsPeer::translateFieldName($name, $type, BasePeer::TYPE_NUM);
        $field = $this->getByPosition($pos);

        return $field;
    }

    /**
     * Retrieves a field from the object by Position as specified in the xml schema.
     * Zero-based.
     *
     * @param int $pos position in xml schema
     * @return mixed Value of field at $pos
     */
    public function getByPosition($pos)
    {
        switch ($pos) {
            case 0:
                return $this->getId();
                break;
            case 1:
                return $this->getName();
                break;
            case 2:
                return $this->getDescription();
                break;
            case 3:
                return $this->getAppId();
                break;
            case 4:
                return $this->getAppKey();
                break;
            case 5:
                return $this->getAdminKey();
                break;
            case 6:
                return $this->getToModified();
                break;
            case 7:
                return $this->getActive();
                break;
            case 8:
                return $this->getLastModified();
                break;
            case 9:
                return $this->getCreated();
                break;
            default:
                return null;
                break;
        } // switch()
    }

    /**
     * Exports the object as an array.
     *
     * You can specify the key type of the array by passing one of the class
     * type constants.
     *
     * @param     string  $keyType (optional) One of the class type constants BasePeer::TYPE_PHPNAME, BasePeer::TYPE_STUDLYPHPNAME,
     *                    BasePeer::TYPE_COLNAME, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_NUM.
     *                    Defaults to BasePeer::TYPE_PHPNAME.
     * @param     boolean $includeLazyLoadColumns (optional) Whether to include lazy loaded columns. Defaults to true.
     * @param     array $alreadyDumpedObjects List of objects to skip to avoid recursion
     * @param     boolean $includeForeignObjects (optional) Whether to include hydrated related objects. Default to FALSE.
     *
     * @return array an associative array containing the field names (as keys) and field values
     */
    public function toArray($keyType = BasePeer::TYPE_PHPNAME, $includeLazyLoadColumns = true, $alreadyDumpedObjects = array(), $includeForeignObjects = false)
    {
        if (isset($alreadyDumpedObjects['Applications'][$this->getPrimaryKey()])) {
            return '*RECURSION*';
        }
        $alreadyDumpedObjects['Applications'][$this->getPrimaryKey()] = true;
        $keys = ApplicationsPeer::getFieldNames($keyType);
        $result = array(
            $keys[0] => $this->getId(),
            $keys[1] => $this->getName(),
            $keys[2] => $this->getDescription(),
            $keys[3] => $this->getAppId(),
            $keys[4] => $this->getAppKey(),
            $keys[5] => $this->getAdminKey(),
            $keys[6] => $this->getToModified(),
            $keys[7] => $this->getActive(),
            $keys[8] => $this->getLastModified(),
            $keys[9] => $this->getCreated(),
        );
        if ($includeForeignObjects) {
            if (null !== $this->collUserPulpits) {
                $result['UserPulpits'] = $this->collUserPulpits->toArray(null, true, $keyType, $includeLazyLoadColumns, $alreadyDumpedObjects);
            }
            if (null !== $this->collApplicationDataSetss) {
                $result['ApplicationDataSetss'] = $this->collApplicationDataSetss->toArray(null, true, $keyType, $includeLazyLoadColumns, $alreadyDumpedObjects);
            }
            if (null !== $this->collUserGroupApplicationss) {
                $result['UserGroupApplicationss'] = $this->collUserGroupApplicationss->toArray(null, true, $keyType, $includeLazyLoadColumns, $alreadyDumpedObjects);
            }
        }

        return $result;
    }

    /**
     * Sets a field from the object by name passed in as a string.
     *
     * @param string $name peer name
     * @param mixed $value field value
     * @param string $type The type of fieldname the $name is of:
     *                     one of the class type constants BasePeer::TYPE_PHPNAME, BasePeer::TYPE_STUDLYPHPNAME
     *                     BasePeer::TYPE_COLNAME, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_NUM.
     *                     Defaults to BasePeer::TYPE_PHPNAME
     * @return void
     */
    public function setByName($name, $value, $type = BasePeer::TYPE_PHPNAME)
    {
        $pos = ApplicationsPeer::translateFieldName($name, $type, BasePeer::TYPE_NUM);

        $this->setByPosition($pos, $value);
    }

    /**
     * Sets a field from the object by Position as specified in the xml schema.
     * Zero-based.
     *
     * @param int $pos position in xml schema
     * @param mixed $value field value
     * @return void
     */
    public function setByPosition($pos, $value)
    {
        switch ($pos) {
            case 0:
                $this->setId($value);
                break;
            case 1:
                $this->setName($value);
                break;
            case 2:
                $this->setDescription($value);
                break;
            case 3:
                $this->setAppId($value);
                break;
            case 4:
                $this->setAppKey($value);
                break;
            case 5:
                $this->setAdminKey($value);
                break;
            case 6:
                $this->setToModified($value);
                break;
            case 7:
                $this->setActive($value);
                break;
            case 8:
                $this->setLastModified($value);
                break;
            case 9:
                $this->setCreated($value);
                break;
        } // switch()
    }

    /**
     * Populates the object using an array.
     *
     * This is particularly useful when populating an object from one of the
     * request arrays (e.g. $_POST).  This method goes through the column
     * names, checking to see whether a matching key exists in populated
     * array. If so the setByName() method is called for that column.
     *
     * You can specify the key type of the array by additionally passing one
     * of the class type constants BasePeer::TYPE_PHPNAME, BasePeer::TYPE_STUDLYPHPNAME,
     * BasePeer::TYPE_COLNAME, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_NUM.
     * The default key type is the column's BasePeer::TYPE_PHPNAME
     *
     * @param array  $arr     An array to populate the object from.
     * @param string $keyType The type of keys the array uses.
     * @return void
     */
    public function fromArray($arr, $keyType = BasePeer::TYPE_PHPNAME)
    {
        $keys = ApplicationsPeer::getFieldNames($keyType);

        if (array_key_exists($keys[0], $arr)) $this->setId($arr[$keys[0]]);
        if (array_key_exists($keys[1], $arr)) $this->setName($arr[$keys[1]]);
        if (array_key_exists($keys[2], $arr)) $this->setDescription($arr[$keys[2]]);
        if (array_key_exists($keys[3], $arr)) $this->setAppId($arr[$keys[3]]);
        if (array_key_exists($keys[4], $arr)) $this->setAppKey($arr[$keys[4]]);
        if (array_key_exists($keys[5], $arr)) $this->setAdminKey($arr[$keys[5]]);
        if (array_key_exists($keys[6], $arr)) $this->setToModified($arr[$keys[6]]);
        if (array_key_exists($keys[7], $arr)) $this->setActive($arr[$keys[7]]);
        if (array_key_exists($keys[8], $arr)) $this->setLastModified($arr[$keys[8]]);
        if (array_key_exists($keys[9], $arr)) $this->setCreated($arr[$keys[9]]);
    }

    /**
     * Build a Criteria object containing the values of all modified columns in this object.
     *
     * @return Criteria The Criteria object containing all modified values.
     */
    public function buildCriteria()
    {
        $criteria = new Criteria(ApplicationsPeer::DATABASE_NAME);

        if ($this->isColumnModified(ApplicationsPeer::ID)) $criteria->add(ApplicationsPeer::ID, $this->id);
        if ($this->isColumnModified(ApplicationsPeer::NAME)) $criteria->add(ApplicationsPeer::NAME, $this->name);
        if ($this->isColumnModified(ApplicationsPeer::DESCRIPTION)) $criteria->add(ApplicationsPeer::DESCRIPTION, $this->description);
        if ($this->isColumnModified(ApplicationsPeer::APP_ID)) $criteria->add(ApplicationsPeer::APP_ID, $this->app_id);
        if ($this->isColumnModified(ApplicationsPeer::APP_KEY)) $criteria->add(ApplicationsPeer::APP_KEY, $this->app_key);
        if ($this->isColumnModified(ApplicationsPeer::ADMIN_KEY)) $criteria->add(ApplicationsPeer::ADMIN_KEY, $this->admin_key);
        if ($this->isColumnModified(ApplicationsPeer::TO_MODIFIED)) $criteria->add(ApplicationsPeer::TO_MODIFIED, $this->to_modified);
        if ($this->isColumnModified(ApplicationsPeer::ACTIVE)) $criteria->add(ApplicationsPeer::ACTIVE, $this->active);
        if ($this->isColumnModified(ApplicationsPeer::LAST_MODIFIED)) $criteria->add(ApplicationsPeer::LAST_MODIFIED, $this->last_modified);
        if ($this->isColumnModified(ApplicationsPeer::CREATED)) $criteria->add(ApplicationsPeer::CREATED, $this->created);

        return $criteria;
    }

    /**
     * Builds a Criteria object containing the primary key for this object.
     *
     * Unlike buildCriteria() this method includes the primary key values regardless
     * of whether or not they have been modified.
     *
     * @return Criteria The Criteria object containing value(s) for primary key(s).
     */
    public function buildPkeyCriteria()
    {
        $criteria = new Criteria(ApplicationsPeer::DATABASE_NAME);
        $criteria->add(ApplicationsPeer::ID, $this->id);

        return $criteria;
    }

    /**
     * Returns the primary key for this object (row).
     * @return int
     */
    public function getPrimaryKey()
    {
        return $this->getId();
    }

    /**
     * Generic method to set the primary key (id column).
     *
     * @param  int $key Primary key.
     * @return void
     */
    public function setPrimaryKey($key)
    {
        $this->setId($key);
    }

    /**
     * Returns true if the primary key for this object is null.
     * @return boolean
     */
    public function isPrimaryKeyNull()
    {

        return null === $this->getId();
    }

    /**
     * Sets contents of passed object to values from current object.
     *
     * If desired, this method can also make copies of all associated (fkey referrers)
     * objects.
     *
     * @param object $copyObj An object of Applications (or compatible) type.
     * @param boolean $deepCopy Whether to also copy all rows that refer (by fkey) to the current row.
     * @param boolean $makeNew Whether to reset autoincrement PKs and make the object new.
     * @throws PropelException
     */
    public function copyInto($copyObj, $deepCopy = false, $makeNew = true)
    {
        $copyObj->setName($this->getName());
        $copyObj->setDescription($this->getDescription());
        $copyObj->setAppId($this->getAppId());
        $copyObj->setAppKey($this->getAppKey());
        $copyObj->setAdminKey($this->getAdminKey());
        $copyObj->setToModified($this->getToModified());
        $copyObj->setActive($this->getActive());
        $copyObj->setLastModified($this->getLastModified());
        $copyObj->setCreated($this->getCreated());

        if ($deepCopy && !$this->startCopy) {
            // important: temporarily setNew(false) because this affects the behavior of
            // the getter/setter methods for fkey referrer objects.
            $copyObj->setNew(false);
            // store object hash to prevent cycle
            $this->startCopy = true;

            foreach ($this->getUserPulpits() as $relObj) {
                if ($relObj !== $this) {  // ensure that we don't try to copy a reference to ourselves
                    $copyObj->addUserPulpit($relObj->copy($deepCopy));
                }
            }

            foreach ($this->getApplicationDataSetss() as $relObj) {
                if ($relObj !== $this) {  // ensure that we don't try to copy a reference to ourselves
                    $copyObj->addApplicationDataSets($relObj->copy($deepCopy));
                }
            }

            foreach ($this->getUserGroupApplicationss() as $relObj) {
                if ($relObj !== $this) {  // ensure that we don't try to copy a reference to ourselves
                    $copyObj->addUserGroupApplications($relObj->copy($deepCopy));
                }
            }

            //unflag object copy
            $this->startCopy = false;
        } // if ($deepCopy)

        if ($makeNew) {
            $copyObj->setNew(true);
            $copyObj->setId(NULL); // this is a auto-increment column, so set to default value
        }
    }

    /**
     * Makes a copy of this object that will be inserted as a new row in table when saved.
     * It creates a new object filling in the simple attributes, but skipping any primary
     * keys that are defined for the table.
     *
     * If desired, this method can also make copies of all associated (fkey referrers)
     * objects.
     *
     * @param boolean $deepCopy Whether to also copy all rows that refer (by fkey) to the current row.
     * @return Applications Clone of current object.
     * @throws PropelException
     */
    public function copy($deepCopy = false)
    {
        // we use get_class(), because this might be a subclass
        $clazz = get_class($this);
        $copyObj = new $clazz();
        $this->copyInto($copyObj, $deepCopy);

        return $copyObj;
    }

    /**
     * Returns a peer instance associated with this om.
     *
     * Since Peer classes are not to have any instance attributes, this method returns the
     * same instance for all member of this class. The method could therefore
     * be static, but this would prevent one from overriding the behavior.
     *
     * @return ApplicationsPeer
     */
    public function getPeer()
    {
        if (self::$peer === null) {
            self::$peer = new ApplicationsPeer();
        }

        return self::$peer;
    }


    /**
     * Initializes a collection based on the name of a relation.
     * Avoids crafting an 'init[$relationName]s' method name
     * that wouldn't work when StandardEnglishPluralizer is used.
     *
     * @param string $relationName The name of the relation to initialize
     * @return void
     */
    public function initRelation($relationName)
    {
        if ('UserPulpit' == $relationName) {
            $this->initUserPulpits();
        }
        if ('ApplicationDataSets' == $relationName) {
            $this->initApplicationDataSetss();
        }
        if ('UserGroupApplications' == $relationName) {
            $this->initUserGroupApplicationss();
        }
    }

    /**
     * Clears out the collUserPulpits collection
     *
     * This does not modify the database; however, it will remove any associated objects, causing
     * them to be refetched by subsequent calls to accessor method.
     *
     * @return void
     * @see        addUserPulpits()
     */
    public function clearUserPulpits()
    {
        $this->collUserPulpits = null; // important to set this to null since that means it is uninitialized
        $this->collUserPulpitsPartial = null;
    }

    /**
     * reset is the collUserPulpits collection loaded partially
     *
     * @return void
     */
    public function resetPartialUserPulpits($v = true)
    {
        $this->collUserPulpitsPartial = $v;
    }

    /**
     * Initializes the collUserPulpits collection.
     *
     * By default this just sets the collUserPulpits collection to an empty array (like clearcollUserPulpits());
     * however, you may wish to override this method in your stub class to provide setting appropriate
     * to your application -- for example, setting the initial array to the values stored in database.
     *
     * @param boolean $overrideExisting If set to true, the method call initializes
     *                                        the collection even if it is not empty
     *
     * @return void
     */
    public function initUserPulpits($overrideExisting = true)
    {
        if (null !== $this->collUserPulpits && !$overrideExisting) {
            return;
        }
        $this->collUserPulpits = new PropelObjectCollection();
        $this->collUserPulpits->setModel('UserPulpit');
    }

    /**
     * Gets an array of UserPulpit objects which contain a foreign key that references this object.
     *
     * If the $criteria is not null, it is used to always fetch the results from the database.
     * Otherwise the results are fetched from the database the first time, then cached.
     * Next time the same method is called without $criteria, the cached collection is returned.
     * If this Applications is new, it will return
     * an empty collection or the current collection; the criteria is ignored on a new object.
     *
     * @param Criteria $criteria optional Criteria object to narrow the query
     * @param PropelPDO $con optional connection object
     * @return PropelObjectCollection|UserPulpit[] List of UserPulpit objects
     * @throws PropelException
     */
    public function getUserPulpits($criteria = null, PropelPDO $con = null)
    {
        $partial = $this->collUserPulpitsPartial && !$this->isNew();
        if (null === $this->collUserPulpits || null !== $criteria  || $partial) {
            if ($this->isNew() && null === $this->collUserPulpits) {
                // return empty collection
                $this->initUserPulpits();
            } else {
                $collUserPulpits = UserPulpitQuery::create(null, $criteria)
                    ->filterByApplications($this)
                    ->find($con);
                if (null !== $criteria) {
                    if (false !== $this->collUserPulpitsPartial && count($collUserPulpits)) {
                      $this->initUserPulpits(false);

                      foreach($collUserPulpits as $obj) {
                        if (false == $this->collUserPulpits->contains($obj)) {
                          $this->collUserPulpits->append($obj);
                        }
                      }

                      $this->collUserPulpitsPartial = true;
                    }

                    return $collUserPulpits;
                }

                if($partial && $this->collUserPulpits) {
                    foreach($this->collUserPulpits as $obj) {
                        if($obj->isNew()) {
                            $collUserPulpits[] = $obj;
                        }
                    }
                }

                $this->collUserPulpits = $collUserPulpits;
                $this->collUserPulpitsPartial = false;
            }
        }

        return $this->collUserPulpits;
    }

    /**
     * Sets a collection of UserPulpit objects related by a one-to-many relationship
     * to the current object.
     * It will also schedule objects for deletion based on a diff between old objects (aka persisted)
     * and new objects from the given Propel collection.
     *
     * @param PropelCollection $userPulpits A Propel collection.
     * @param PropelPDO $con Optional connection object
     */
    public function setUserPulpits(PropelCollection $userPulpits, PropelPDO $con = null)
    {
        $this->userPulpitsScheduledForDeletion = $this->getUserPulpits(new Criteria(), $con)->diff($userPulpits);

        foreach ($this->userPulpitsScheduledForDeletion as $userPulpitRemoved) {
            $userPulpitRemoved->setApplications(null);
        }

        $this->collUserPulpits = null;
        foreach ($userPulpits as $userPulpit) {
            $this->addUserPulpit($userPulpit);
        }

        $this->collUserPulpits = $userPulpits;
        $this->collUserPulpitsPartial = false;
    }

    /**
     * Returns the number of related UserPulpit objects.
     *
     * @param Criteria $criteria
     * @param boolean $distinct
     * @param PropelPDO $con
     * @return int             Count of related UserPulpit objects.
     * @throws PropelException
     */
    public function countUserPulpits(Criteria $criteria = null, $distinct = false, PropelPDO $con = null)
    {
        $partial = $this->collUserPulpitsPartial && !$this->isNew();
        if (null === $this->collUserPulpits || null !== $criteria || $partial) {
            if ($this->isNew() && null === $this->collUserPulpits) {
                return 0;
            } else {
                if($partial && !$criteria) {
                    return count($this->getUserPulpits());
                }
                $query = UserPulpitQuery::create(null, $criteria);
                if ($distinct) {
                    $query->distinct();
                }

                return $query
                    ->filterByApplications($this)
                    ->count($con);
            }
        } else {
            return count($this->collUserPulpits);
        }
    }

    /**
     * Method called to associate a UserPulpit object to this object
     * through the UserPulpit foreign key attribute.
     *
     * @param    UserPulpit $l UserPulpit
     * @return Applications The current object (for fluent API support)
     */
    public function addUserPulpit(UserPulpit $l)
    {
        if ($this->collUserPulpits === null) {
            $this->initUserPulpits();
            $this->collUserPulpitsPartial = true;
        }
        if (!$this->collUserPulpits->contains($l)) { // only add it if the **same** object is not already associated
            $this->doAddUserPulpit($l);
        }

        return $this;
    }

    /**
     * @param	UserPulpit $userPulpit The userPulpit object to add.
     */
    protected function doAddUserPulpit($userPulpit)
    {
        $this->collUserPulpits[]= $userPulpit;
        $userPulpit->setApplications($this);
    }

    /**
     * @param	UserPulpit $userPulpit The userPulpit object to remove.
     */
    public function removeUserPulpit($userPulpit)
    {
        if ($this->getUserPulpits()->contains($userPulpit)) {
            $this->collUserPulpits->remove($this->collUserPulpits->search($userPulpit));
            if (null === $this->userPulpitsScheduledForDeletion) {
                $this->userPulpitsScheduledForDeletion = clone $this->collUserPulpits;
                $this->userPulpitsScheduledForDeletion->clear();
            }
            $this->userPulpitsScheduledForDeletion[]= $userPulpit;
            $userPulpit->setApplications(null);
        }
    }


    /**
     * If this collection has already been initialized with
     * an identical criteria, it returns the collection.
     * Otherwise if this Applications is new, it will return
     * an empty collection; or if this Applications has previously
     * been saved, it will retrieve related UserPulpits from storage.
     *
     * This method is protected by default in order to keep the public
     * api reasonable.  You can provide public methods for those you
     * actually need in Applications.
     *
     * @param Criteria $criteria optional Criteria object to narrow the query
     * @param PropelPDO $con optional connection object
     * @param string $join_behavior optional join type to use (defaults to Criteria::LEFT_JOIN)
     * @return PropelObjectCollection|UserPulpit[] List of UserPulpit objects
     */
    public function getUserPulpitsJoinUsers($criteria = null, $con = null, $join_behavior = Criteria::LEFT_JOIN)
    {
        $query = UserPulpitQuery::create(null, $criteria);
        $query->joinWith('Users', $join_behavior);

        return $this->getUserPulpits($query, $con);
    }

    /**
     * Clears out the collApplicationDataSetss collection
     *
     * This does not modify the database; however, it will remove any associated objects, causing
     * them to be refetched by subsequent calls to accessor method.
     *
     * @return void
     * @see        addApplicationDataSetss()
     */
    public function clearApplicationDataSetss()
    {
        $this->collApplicationDataSetss = null; // important to set this to null since that means it is uninitialized
        $this->collApplicationDataSetssPartial = null;
    }

    /**
     * reset is the collApplicationDataSetss collection loaded partially
     *
     * @return void
     */
    public function resetPartialApplicationDataSetss($v = true)
    {
        $this->collApplicationDataSetssPartial = $v;
    }

    /**
     * Initializes the collApplicationDataSetss collection.
     *
     * By default this just sets the collApplicationDataSetss collection to an empty array (like clearcollApplicationDataSetss());
     * however, you may wish to override this method in your stub class to provide setting appropriate
     * to your application -- for example, setting the initial array to the values stored in database.
     *
     * @param boolean $overrideExisting If set to true, the method call initializes
     *                                        the collection even if it is not empty
     *
     * @return void
     */
    public function initApplicationDataSetss($overrideExisting = true)
    {
        if (null !== $this->collApplicationDataSetss && !$overrideExisting) {
            return;
        }
        $this->collApplicationDataSetss = new PropelObjectCollection();
        $this->collApplicationDataSetss->setModel('ApplicationDataSets');
    }

    /**
     * Gets an array of ApplicationDataSets objects which contain a foreign key that references this object.
     *
     * If the $criteria is not null, it is used to always fetch the results from the database.
     * Otherwise the results are fetched from the database the first time, then cached.
     * Next time the same method is called without $criteria, the cached collection is returned.
     * If this Applications is new, it will return
     * an empty collection or the current collection; the criteria is ignored on a new object.
     *
     * @param Criteria $criteria optional Criteria object to narrow the query
     * @param PropelPDO $con optional connection object
     * @return PropelObjectCollection|ApplicationDataSets[] List of ApplicationDataSets objects
     * @throws PropelException
     */
    public function getApplicationDataSetss($criteria = null, PropelPDO $con = null)
    {
        $partial = $this->collApplicationDataSetssPartial && !$this->isNew();
        if (null === $this->collApplicationDataSetss || null !== $criteria  || $partial) {
            if ($this->isNew() && null === $this->collApplicationDataSetss) {
                // return empty collection
                $this->initApplicationDataSetss();
            } else {
                $collApplicationDataSetss = ApplicationDataSetsQuery::create(null, $criteria)
                    ->filterByApplications($this)
                    ->find($con);
                if (null !== $criteria) {
                    if (false !== $this->collApplicationDataSetssPartial && count($collApplicationDataSetss)) {
                      $this->initApplicationDataSetss(false);

                      foreach($collApplicationDataSetss as $obj) {
                        if (false == $this->collApplicationDataSetss->contains($obj)) {
                          $this->collApplicationDataSetss->append($obj);
                        }
                      }

                      $this->collApplicationDataSetssPartial = true;
                    }

                    return $collApplicationDataSetss;
                }

                if($partial && $this->collApplicationDataSetss) {
                    foreach($this->collApplicationDataSetss as $obj) {
                        if($obj->isNew()) {
                            $collApplicationDataSetss[] = $obj;
                        }
                    }
                }

                $this->collApplicationDataSetss = $collApplicationDataSetss;
                $this->collApplicationDataSetssPartial = false;
            }
        }

        return $this->collApplicationDataSetss;
    }

    /**
     * Sets a collection of ApplicationDataSets objects related by a one-to-many relationship
     * to the current object.
     * It will also schedule objects for deletion based on a diff between old objects (aka persisted)
     * and new objects from the given Propel collection.
     *
     * @param PropelCollection $applicationDataSetss A Propel collection.
     * @param PropelPDO $con Optional connection object
     */
    public function setApplicationDataSetss(PropelCollection $applicationDataSetss, PropelPDO $con = null)
    {
        $this->applicationDataSetssScheduledForDeletion = $this->getApplicationDataSetss(new Criteria(), $con)->diff($applicationDataSetss);

        foreach ($this->applicationDataSetssScheduledForDeletion as $applicationDataSetsRemoved) {
            $applicationDataSetsRemoved->setApplications(null);
        }

        $this->collApplicationDataSetss = null;
        foreach ($applicationDataSetss as $applicationDataSets) {
            $this->addApplicationDataSets($applicationDataSets);
        }

        $this->collApplicationDataSetss = $applicationDataSetss;
        $this->collApplicationDataSetssPartial = false;
    }

    /**
     * Returns the number of related ApplicationDataSets objects.
     *
     * @param Criteria $criteria
     * @param boolean $distinct
     * @param PropelPDO $con
     * @return int             Count of related ApplicationDataSets objects.
     * @throws PropelException
     */
    public function countApplicationDataSetss(Criteria $criteria = null, $distinct = false, PropelPDO $con = null)
    {
        $partial = $this->collApplicationDataSetssPartial && !$this->isNew();
        if (null === $this->collApplicationDataSetss || null !== $criteria || $partial) {
            if ($this->isNew() && null === $this->collApplicationDataSetss) {
                return 0;
            } else {
                if($partial && !$criteria) {
                    return count($this->getApplicationDataSetss());
                }
                $query = ApplicationDataSetsQuery::create(null, $criteria);
                if ($distinct) {
                    $query->distinct();
                }

                return $query
                    ->filterByApplications($this)
                    ->count($con);
            }
        } else {
            return count($this->collApplicationDataSetss);
        }
    }

    /**
     * Method called to associate a ApplicationDataSets object to this object
     * through the ApplicationDataSets foreign key attribute.
     *
     * @param    ApplicationDataSets $l ApplicationDataSets
     * @return Applications The current object (for fluent API support)
     */
    public function addApplicationDataSets(ApplicationDataSets $l)
    {
        if ($this->collApplicationDataSetss === null) {
            $this->initApplicationDataSetss();
            $this->collApplicationDataSetssPartial = true;
        }
        if (!$this->collApplicationDataSetss->contains($l)) { // only add it if the **same** object is not already associated
            $this->doAddApplicationDataSets($l);
        }

        return $this;
    }

    /**
     * @param	ApplicationDataSets $applicationDataSets The applicationDataSets object to add.
     */
    protected function doAddApplicationDataSets($applicationDataSets)
    {
        $this->collApplicationDataSetss[]= $applicationDataSets;
        $applicationDataSets->setApplications($this);
    }

    /**
     * @param	ApplicationDataSets $applicationDataSets The applicationDataSets object to remove.
     */
    public function removeApplicationDataSets($applicationDataSets)
    {
        if ($this->getApplicationDataSetss()->contains($applicationDataSets)) {
            $this->collApplicationDataSetss->remove($this->collApplicationDataSetss->search($applicationDataSets));
            if (null === $this->applicationDataSetssScheduledForDeletion) {
                $this->applicationDataSetssScheduledForDeletion = clone $this->collApplicationDataSetss;
                $this->applicationDataSetssScheduledForDeletion->clear();
            }
            $this->applicationDataSetssScheduledForDeletion[]= $applicationDataSets;
            $applicationDataSets->setApplications(null);
        }
    }

    /**
     * Clears out the collUserGroupApplicationss collection
     *
     * This does not modify the database; however, it will remove any associated objects, causing
     * them to be refetched by subsequent calls to accessor method.
     *
     * @return void
     * @see        addUserGroupApplicationss()
     */
    public function clearUserGroupApplicationss()
    {
        $this->collUserGroupApplicationss = null; // important to set this to null since that means it is uninitialized
        $this->collUserGroupApplicationssPartial = null;
    }

    /**
     * reset is the collUserGroupApplicationss collection loaded partially
     *
     * @return void
     */
    public function resetPartialUserGroupApplicationss($v = true)
    {
        $this->collUserGroupApplicationssPartial = $v;
    }

    /**
     * Initializes the collUserGroupApplicationss collection.
     *
     * By default this just sets the collUserGroupApplicationss collection to an empty array (like clearcollUserGroupApplicationss());
     * however, you may wish to override this method in your stub class to provide setting appropriate
     * to your application -- for example, setting the initial array to the values stored in database.
     *
     * @param boolean $overrideExisting If set to true, the method call initializes
     *                                        the collection even if it is not empty
     *
     * @return void
     */
    public function initUserGroupApplicationss($overrideExisting = true)
    {
        if (null !== $this->collUserGroupApplicationss && !$overrideExisting) {
            return;
        }
        $this->collUserGroupApplicationss = new PropelObjectCollection();
        $this->collUserGroupApplicationss->setModel('UserGroupApplications');
    }

    /**
     * Gets an array of UserGroupApplications objects which contain a foreign key that references this object.
     *
     * If the $criteria is not null, it is used to always fetch the results from the database.
     * Otherwise the results are fetched from the database the first time, then cached.
     * Next time the same method is called without $criteria, the cached collection is returned.
     * If this Applications is new, it will return
     * an empty collection or the current collection; the criteria is ignored on a new object.
     *
     * @param Criteria $criteria optional Criteria object to narrow the query
     * @param PropelPDO $con optional connection object
     * @return PropelObjectCollection|UserGroupApplications[] List of UserGroupApplications objects
     * @throws PropelException
     */
    public function getUserGroupApplicationss($criteria = null, PropelPDO $con = null)
    {
        $partial = $this->collUserGroupApplicationssPartial && !$this->isNew();
        if (null === $this->collUserGroupApplicationss || null !== $criteria  || $partial) {
            if ($this->isNew() && null === $this->collUserGroupApplicationss) {
                // return empty collection
                $this->initUserGroupApplicationss();
            } else {
                $collUserGroupApplicationss = UserGroupApplicationsQuery::create(null, $criteria)
                    ->filterByApplications($this)
                    ->find($con);
                if (null !== $criteria) {
                    if (false !== $this->collUserGroupApplicationssPartial && count($collUserGroupApplicationss)) {
                      $this->initUserGroupApplicationss(false);

                      foreach($collUserGroupApplicationss as $obj) {
                        if (false == $this->collUserGroupApplicationss->contains($obj)) {
                          $this->collUserGroupApplicationss->append($obj);
                        }
                      }

                      $this->collUserGroupApplicationssPartial = true;
                    }

                    return $collUserGroupApplicationss;
                }

                if($partial && $this->collUserGroupApplicationss) {
                    foreach($this->collUserGroupApplicationss as $obj) {
                        if($obj->isNew()) {
                            $collUserGroupApplicationss[] = $obj;
                        }
                    }
                }

                $this->collUserGroupApplicationss = $collUserGroupApplicationss;
                $this->collUserGroupApplicationssPartial = false;
            }
        }

        return $this->collUserGroupApplicationss;
    }

    /**
     * Sets a collection of UserGroupApplications objects related by a one-to-many relationship
     * to the current object.
     * It will also schedule objects for deletion based on a diff between old objects (aka persisted)
     * and new objects from the given Propel collection.
     *
     * @param PropelCollection $userGroupApplicationss A Propel collection.
     * @param PropelPDO $con Optional connection object
     */
    public function setUserGroupApplicationss(PropelCollection $userGroupApplicationss, PropelPDO $con = null)
    {
        $this->userGroupApplicationssScheduledForDeletion = $this->getUserGroupApplicationss(new Criteria(), $con)->diff($userGroupApplicationss);

        foreach ($this->userGroupApplicationssScheduledForDeletion as $userGroupApplicationsRemoved) {
            $userGroupApplicationsRemoved->setApplications(null);
        }

        $this->collUserGroupApplicationss = null;
        foreach ($userGroupApplicationss as $userGroupApplications) {
            $this->addUserGroupApplications($userGroupApplications);
        }

        $this->collUserGroupApplicationss = $userGroupApplicationss;
        $this->collUserGroupApplicationssPartial = false;
    }

    /**
     * Returns the number of related UserGroupApplications objects.
     *
     * @param Criteria $criteria
     * @param boolean $distinct
     * @param PropelPDO $con
     * @return int             Count of related UserGroupApplications objects.
     * @throws PropelException
     */
    public function countUserGroupApplicationss(Criteria $criteria = null, $distinct = false, PropelPDO $con = null)
    {
        $partial = $this->collUserGroupApplicationssPartial && !$this->isNew();
        if (null === $this->collUserGroupApplicationss || null !== $criteria || $partial) {
            if ($this->isNew() && null === $this->collUserGroupApplicationss) {
                return 0;
            } else {
                if($partial && !$criteria) {
                    return count($this->getUserGroupApplicationss());
                }
                $query = UserGroupApplicationsQuery::create(null, $criteria);
                if ($distinct) {
                    $query->distinct();
                }

                return $query
                    ->filterByApplications($this)
                    ->count($con);
            }
        } else {
            return count($this->collUserGroupApplicationss);
        }
    }

    /**
     * Method called to associate a UserGroupApplications object to this object
     * through the UserGroupApplications foreign key attribute.
     *
     * @param    UserGroupApplications $l UserGroupApplications
     * @return Applications The current object (for fluent API support)
     */
    public function addUserGroupApplications(UserGroupApplications $l)
    {
        if ($this->collUserGroupApplicationss === null) {
            $this->initUserGroupApplicationss();
            $this->collUserGroupApplicationssPartial = true;
        }
        if (!$this->collUserGroupApplicationss->contains($l)) { // only add it if the **same** object is not already associated
            $this->doAddUserGroupApplications($l);
        }

        return $this;
    }

    /**
     * @param	UserGroupApplications $userGroupApplications The userGroupApplications object to add.
     */
    protected function doAddUserGroupApplications($userGroupApplications)
    {
        $this->collUserGroupApplicationss[]= $userGroupApplications;
        $userGroupApplications->setApplications($this);
    }

    /**
     * @param	UserGroupApplications $userGroupApplications The userGroupApplications object to remove.
     */
    public function removeUserGroupApplications($userGroupApplications)
    {
        if ($this->getUserGroupApplicationss()->contains($userGroupApplications)) {
            $this->collUserGroupApplicationss->remove($this->collUserGroupApplicationss->search($userGroupApplications));
            if (null === $this->userGroupApplicationssScheduledForDeletion) {
                $this->userGroupApplicationssScheduledForDeletion = clone $this->collUserGroupApplicationss;
                $this->userGroupApplicationssScheduledForDeletion->clear();
            }
            $this->userGroupApplicationssScheduledForDeletion[]= $userGroupApplications;
            $userGroupApplications->setApplications(null);
        }
    }


    /**
     * If this collection has already been initialized with
     * an identical criteria, it returns the collection.
     * Otherwise if this Applications is new, it will return
     * an empty collection; or if this Applications has previously
     * been saved, it will retrieve related UserGroupApplicationss from storage.
     *
     * This method is protected by default in order to keep the public
     * api reasonable.  You can provide public methods for those you
     * actually need in Applications.
     *
     * @param Criteria $criteria optional Criteria object to narrow the query
     * @param PropelPDO $con optional connection object
     * @param string $join_behavior optional join type to use (defaults to Criteria::LEFT_JOIN)
     * @return PropelObjectCollection|UserGroupApplications[] List of UserGroupApplications objects
     */
    public function getUserGroupApplicationssJoinUserGroups($criteria = null, $con = null, $join_behavior = Criteria::LEFT_JOIN)
    {
        $query = UserGroupApplicationsQuery::create(null, $criteria);
        $query->joinWith('UserGroups', $join_behavior);

        return $this->getUserGroupApplicationss($query, $con);
    }

    /**
     * Clears the current object and sets all attributes to their default values
     */
    public function clear()
    {
        $this->id = null;
        $this->name = null;
        $this->description = null;
        $this->app_id = null;
        $this->app_key = null;
        $this->admin_key = null;
        $this->to_modified = null;
        $this->active = null;
        $this->last_modified = null;
        $this->created = null;
        $this->alreadyInSave = false;
        $this->alreadyInValidation = false;
        $this->clearAllReferences();
        $this->applyDefaultValues();
        $this->resetModified();
        $this->setNew(true);
        $this->setDeleted(false);
    }

    /**
     * Resets all references to other model objects or collections of model objects.
     *
     * This method is a user-space workaround for PHP's inability to garbage collect
     * objects with circular references (even in PHP 5.3). This is currently necessary
     * when using Propel in certain daemon or large-volumne/high-memory operations.
     *
     * @param boolean $deep Whether to also clear the references on all referrer objects.
     */
    public function clearAllReferences($deep = false)
    {
        if ($deep) {
            if ($this->collUserPulpits) {
                foreach ($this->collUserPulpits as $o) {
                    $o->clearAllReferences($deep);
                }
            }
            if ($this->collApplicationDataSetss) {
                foreach ($this->collApplicationDataSetss as $o) {
                    $o->clearAllReferences($deep);
                }
            }
            if ($this->collUserGroupApplicationss) {
                foreach ($this->collUserGroupApplicationss as $o) {
                    $o->clearAllReferences($deep);
                }
            }
        } // if ($deep)

        if ($this->collUserPulpits instanceof PropelCollection) {
            $this->collUserPulpits->clearIterator();
        }
        $this->collUserPulpits = null;
        if ($this->collApplicationDataSetss instanceof PropelCollection) {
            $this->collApplicationDataSetss->clearIterator();
        }
        $this->collApplicationDataSetss = null;
        if ($this->collUserGroupApplicationss instanceof PropelCollection) {
            $this->collUserGroupApplicationss->clearIterator();
        }
        $this->collUserGroupApplicationss = null;
    }

    /**
     * return the string representation of this object
     *
     * @return string
     */
    public function __toString()
    {
        return (string) $this->exportTo(ApplicationsPeer::DEFAULT_STRING_FORMAT);
    }

    /**
     * return true is the object is in saving state
     *
     * @return boolean
     */
    public function isAlreadyInSave()
    {
        return $this->alreadyInSave;
    }

}
